package com.tianya.controller;

import com.tianya.common.Results;
import com.tianya.domain.dto.CourseOrderRequestDTO;
import com.tianya.domain.entity.CourseOrder;
import com.tianya.domain.vo.CourseOrderVO;
import com.tianya.domain.vo.PageResultVO;
import com.tianya.service.CourseOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * @author TianYaGuoKe
 * @date 2025/5/26 13:33
 * @description CourseOrderController
 */
@Api(tags = "课程订单管理")
@RestController
@RequestMapping("/courseOrder")
public class CourseOrderController {
    @Resource
    private CourseOrderService courseOrderService;

    @ApiOperation("创建课程订单")
    @PostMapping
    public Results<CourseOrder> createCourseOrder(@RequestBody CourseOrderRequestDTO courseOrderRequestDTO) {
        CourseOrder courseOrder = courseOrderService.createCourseOrder(courseOrderRequestDTO);
        return Results.success(courseOrder);
    }

    @ApiOperation("分页获取课程订单")
    @GetMapping("/page")
    public Results<PageResultVO<CourseOrderVO>> pageCourseOrder(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Long pageNum,
            @ApiParam("每页大小") @RequestParam(defaultValue = "10") Long pageSize,
            CourseOrderRequestDTO courseOrderRequestDTO) {
        System.out.println("courseOrderRequestDTO: " + courseOrderRequestDTO);
        PageResultVO<CourseOrderVO> pageResult = courseOrderService.pageCourseOrder(pageNum, pageSize, courseOrderRequestDTO);
        return Results.success(pageResult);
    }

    @ApiOperation("查看订单详情")
    @GetMapping("/{id}")
    public Results<CourseOrder> getCourseOrder(
            @ApiParam("订单ID") @PathVariable Long id) {
        CourseOrder order = courseOrderService.getById(id);
        return order != null ? Results.success(order) : Results.error(404, "订单不存在");
    }

    @ApiOperation("支付订单")
    @PutMapping("/pay/{id}")
    public Results<Boolean> payOrder(
            @ApiParam("订单ID") @PathVariable Long id,
            @ApiParam("家长ID") @RequestParam Long parentId) {
        boolean success = courseOrderService.updateOrderStatus(id, parentId);
        return success ? Results.success(true) : Results.error(400, "支付失败");
    }

    @ApiOperation("更新订单")
    @PutMapping
    public Results<Boolean> updateCourseOrder(@RequestBody CourseOrder courseOrder) {
        boolean success = courseOrderService.updateById(courseOrder);
        return success ? Results.success(true) : Results.error(400, "更新失败");
    }

    @ApiOperation("删除课程订单")
    @DeleteMapping("/{id}")
    public Results<Boolean> deleteCourseOrder(
            @ApiParam("订单ID") @PathVariable Long id) {
        boolean success = courseOrderService.removeById(id);
        return success ? Results.success(true) : Results.error(400, "删除失败");
    }

    @ApiOperation("获取订单详情")
    @GetMapping("/detail/{orderId}")
    public Results<CourseOrder> getOrderDetail(
            @ApiParam("订单ID") @PathVariable Long orderId,
            @ApiParam("家长ID") @RequestParam Long parentId) {
        CourseOrder order = courseOrderService.getOrderDetail(orderId, parentId);
        return order != null ? Results.success(order) : Results.error(404, "订单不存在");
    }

    @ApiOperation("取消订单")
    @PutMapping("/cancel/{id}")
    public Results<Boolean> cancelOrder(
            @ApiParam("订单ID") @PathVariable Long id,
            @ApiParam("家长ID") @RequestParam Long parentId) {
        // 这里需要在service层添加cancelOrder方法
        CourseOrderRequestDTO requestDTO = new CourseOrderRequestDTO();
        requestDTO.setParentId(parentId);
        requestDTO.setId(id);
        boolean success = courseOrderService.cancelOrder(requestDTO);
        return success ? Results.success(true) : Results.error(400, "取消失败");
    }
}